﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;

namespace Ccit.Utils
{
    /// <summary>
    /// 字符串操作类
    /// </summary>
    public class StringLib
    {
        private string _separatorString;
        private char _separator;
        private List<string> _lstString;
        private delegate bool CompareMethod
            (SortOrder sortOrder, SortType sortType, string strA, string strB);

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stringdata">输入的字符串</param>
        public StringLib(string stringdata)
        {
            _separatorString = stringdata;
            _separator = ',';
            InitListArray();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stringdata">输入的字符串</param>
        /// <param name="separator">字符串中数据的分隔符</param>
        public StringLib(string stringdata,char separator)
        {
            _separatorString = stringdata;
            _separator = separator;
            InitListArray();
        }

        private void InitListArray()
        {
            _lstString = new List<string>();
            if(string.IsNullOrEmpty(_separatorString))
            {
                return;
            }
            else
            {
                string[] strArr = _separatorString.Split(_separator);
                foreach(string str in strArr)
                {
                    _lstString.Add(str);
                }
            }
        }

        #endregion

        #region 属性
        /// <summary>
        /// 字符串中数据项的个数
        /// </summary>
        public int ItemCount
        {
            get
            {
                return _lstString.Count;
            }
        }

        /// <summary>
        /// 字符串的数组形式
        /// </summary>
        public List<string> ArrayItem
        {
            get { return _lstString; }
        }
        #endregion

        #region 实例公开接口

        /// <summary>
        /// 返回用分隔符连接后的字符串(分隔符为创建对象时指定，或默认为","分隔)
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            char separator = _separator;
            return ToString(separator);
        }

        /// <summary>
        /// 返回用指定分隔符连接后的字符处
        /// </summary>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public string ToString(char separator)
        {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            foreach (string str in _lstString)
            {
                if (i == 0)
                    sb.Append(str);
                else
                {
                    sb.Append(separator).Append(str);
                }
                i++;
            }

            return sb.ToString();
        }

        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="item"></param>
        public void Add(string item)
        {
            _lstString.Add(item);
            _separatorString = ToString();
        }

        /// <summary>
        /// 删除项
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(string item)
        {
            bool bRemove = _lstString.Remove(item);
            _separatorString = ToString();
            return bRemove;
        }

        /// <summary>
        /// 是否包含指定项
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(string item)
        {
            return _lstString.Contains(item);
        }

        #region 排序

        /// <summary>
        /// 数据项排序
        /// </summary>
        /// <param name="sortOrder">排序顺序(升序，降序)</param>
        /// <param name="sortType">排序类型(以数字，字符串，或ASCII排序)</param>
        /// <returns></returns>
        public string Sort(SortOrder sortOrder,SortType sortType)
        {
            int iCount = ItemCount;
            if (iCount == 0)
                return ToString();

            string strTemp = "";
            CompareMethod compareMehtod;
            compareMehtod = (sortType == SortType.NUMBER) ?
                new CompareMethod(pCompareByNumber) :
                new CompareMethod(pCompareByString);
            for(int i = 0; i < iCount; i ++)
            {
                for(int j = i + 1; j < iCount; j ++)
                {
                    if(!compareMehtod(sortOrder, sortType, _lstString[i], _lstString[j]))
                    {
                        strTemp = _lstString[i];
                        _lstString[i] = _lstString[j];
                        _lstString[j] = strTemp;
                    }
                }
            }

            return ToString();
        }
       
        private bool pCompareByNumber(SortOrder sortOrder,SortType sortType,string strA,string strB)
        {
            int intA = 0;
            int intB = 0;
            bool bCompare = false;
            try
            {
                intA = Int32.Parse(strA);
                intB = Int32.Parse(strB);
                if(sortOrder == SortOrder.Asc)
                {
                    bCompare = (intA <= intB) ? true : false;
                }
                else
                {
                    bCompare = (intA >= intB) ? true : false;
                }
            }
            catch
            {
                throw new Exception("convert to Int32 throw exception");
            }

            return bCompare;
        }

        private bool pCompareByString(SortOrder sortOrder,SortType sortType,string strA,string strB)
        {
            bool bCompare = false;
            int iCompare = 0;

            /* 按字符串字母排列顺序比较 */
            if (sortType == SortType.STRING)
                iCompare = strA.CompareTo(strB);
            else if(sortType == SortType.ASCII)  //按AscII码值比较
            {
                iCompare = string.CompareOrdinal(strA, strB);
            }


            if(sortOrder == SortOrder.Asc)
                bCompare = (iCompare <= 0) ? true : false;
            else
            {
                bCompare = (iCompare > 0) ? true : false;
            }

            return bCompare;
        }

        #endregion

        /// <summary>
        /// 将两个以相同分隔符分隔的字符串无重复项的合并，并返回合并后结果
        /// 如:"1,3,5,7,9" ; "1,5,9,11"
        /// 合并后: "1,3,5,7,9,11"
        /// </summary>
        /// <param name="strother">待合并的字符串</param>
        /// <returns>返回合并后无重复项的字符串</returns>
        public string Merge(string strother)
        {
            if(string.IsNullOrEmpty(_separatorString)) return strother;

            if(string.IsNullOrEmpty(strother)) return _separatorString;
            
            if(_separatorString == strother) return _separatorString;
           
            string tempstr = _separator.ToString() + strother + _separator.ToString();
            int index = 0;

            foreach(string item in _lstString)
            {
                index = tempstr.IndexOf(string.Format(_separator.ToString() + "{0}" + _separator.ToString                         (),item));
                if (index != -1)
                {
                    tempstr = tempstr.Remove(index,item.Length + 1);
                }
            }
            
            tempstr = (tempstr.Substring(tempstr.Length - 1) == _separator.ToString()) ? 
                    tempstr.Substring(0, tempstr.Length - 1) : tempstr;
            return (ToString() + tempstr);

        }

        /// <summary>
        /// 合并两个以不同分隔符分隔的字符串
        /// </summary>
        /// <param name="strother">待合并的字符串</param>
        /// <param name="separator">字符串的分隔符</param>
        /// <returns></returns>
        public string Merge(string strother,char separator)
        {
            StringLib strbOther = new StringLib(strother,separator);
            return Merge(strbOther.ToString(_separator));
        }

        #endregion
    }

    /// <summary>
    /// 排序顺序枚举
    /// </summary>
    public enum SortOrder
    {
        /// <summary>
        /// 升序
        /// </summary>
        Asc = 1,
        /// <summary>
        /// 降序
        /// </summary>
        Desc = 2
    }

    /// <summary>
    /// 要排序的数据类型枚举
    /// </summary>
    public enum SortType
    {
        /// <summary>
        /// 数值型数据
        /// </summary>
        NUMBER = 1,

        /// <summary>
        /// 字符串型数据
        /// </summary>
        STRING = 2,

        /// <summary>
        /// ASCII数据
        /// </summary>
        ASCII = 3
    }
}
